<template>
  <div class="upload-img">
    <el-upload
      action="#"
      :http-request="httpRequest"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
    >
      <div class="main">
        <img v-if="value" :src="value" alt="">
        <i v-else class="el-icon-plus" />
      </div>
    </el-upload>
    <!-- 删除 -->
    <i v-if="value" class="del el-icon-delete" @click="del" />
    <!-- 预览 -->
    <i v-if="value" class="big el-icon-zoom-in" @click="isShow=true" />
    <!-- 大对话框 -->
    <el-dialog title="图片预览" :visible.sync="isShow">
      <img :src="value" alt="">
    </el-dialog>
    <!-- 进度条 -->
    <el-progress
      v-if="showProgress"
      class="progress"
      :width="200"
      type="circle"
      :percentage="percentage"
    />
  </div>

</template>

<script>
import COS from 'cos-js-sdk-v5'

export default {
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      imgSrc: '',
      cos: '',
      showProgress: false,
      isShow: false,
      percentage: 0
    }
  },
  created() {
    this.cos = new COS({
      SecretId: 'AKIDsa5JK9l6Afx0oNkuoVDoJZhuxmNp3GNG',
      SecretKey: 'jOuCDzZw53sW0IUWO6artfCWdV7TedO2'
    })
  },
  methods: {
    httpRequest(res) {
      this.showProgress = true
      this.cos.uploadFile(
        {
          Bucket: 'xqz-1314889045' /* 填写自己的存储桶，必须字段 */,
          Region: 'ap-nanjing' /* 存储桶所在地域，必须字段 */,
          Key: res.file.name /* 存储在桶里的对象键（例如:1.jpg，a/b/test.txt，图片.jpg）支持中文，必须字段 */,
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            this.percentage = +(progressData.percent * 100).toFixed(2)
            if (progressData.percent === 1) {
              this.showProgress = false
            }
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          if (err) {
            console.log('上传失败', err)
          } else {
            this.$emit('input', 'http://' + data.Location)
            console.log('上传成功', data)
          }
        }
      )
    },
    del() {
      this.value = ''
    },
    handleAvatarSuccess(res, file) {
      this.value = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' ||
      file.type === 'image/png' ||
      file.type === 'image/gif'

      const isLt2M = file.size / 1024 / 1024 < 8

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG/PNG/GIF 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 8MB!')
      }
      return isJPG && isLt2M
    }

  }
}

</script>

<style lang='scss' scoped>
.upload-img{
  width: 200px;
  height: 200px;
 position: relative;
 &:hover{
  .del,
  .big{
display: block;
  }
 }
 .del,
 .big{
  position: absolute;
  display: none;
  right:10px;
  top: 5px;
  cursor: pointer;
 }
 .big{
  right:40px;
 }
 .progress{
  position: absolute;
  left: 0;
  top: 0;
background-color: rgba(0,0,0,0.5);
 }
}
.main{
  width: 200px;
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px dashed #ccc;
  i{
    font-size: 30px;
  }
  img{
    width: 100%;
    height: 100%;
  }
}

</style>
